package ambientes;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.text.NumberFormat;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextPane;
import javax.swing.border.BevelBorder;
import javax.swing.border.EtchedBorder;

import net.java.balloontip.BalloonTip;
import net.java.balloontip.BalloonTip.AttachLocation;
import net.java.balloontip.BalloonTip.Orientation;
import net.java.balloontip.styles.BalloonTipStyle;
import net.java.balloontip.styles.RoundedBalloonStyle;
import agentes.PrincipioFundamentalContagem;
import agentes.PrincipioFundamentalContagemDicas;
import agentes.PrincipioFundamentalContagemDicasRegras;
import agentes.PrincipioFundamentalContagemRegras;

public class PrincipioFundamentalContagemInterface extends JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = -646759208217349461L;

	PrincipioFundamentalContagemRegras regras = new PrincipioFundamentalContagemRegras();
	PrincipioFundamentalContagemDicasRegras regrasDicas = new PrincipioFundamentalContagemDicasRegras();
	int erros[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
	
	private JFormattedTextField textField;
	private JFormattedTextField textField_1;
	private JFormattedTextField textField_2;
	private JFormattedTextField textField_3;
	private JFormattedTextField textField_4;

	JLabel lblEnunciado;
	JLabel lblAjuda;

	BalloonTip balloonTip;
	BalloonTip balloonTip1;
	BalloonTip balloonTip2;
	BalloonTip balloonTip1_1;
	BalloonTip balloonTip2_1;
	BalloonTip balloonTip3;

	JButton btnValida1;
	
	JTextPane txtpnDicas;
	
	private JPanel panel;
	private JPanel panel_dica;
	private JPanel panel_2;
	private JButton btnDica;
	private JLabel label;
	private JPanel panel_3;
	private JPanel panel_4;
	private JLabel lblX;
	private JTabbedPane tabbedPane;
	private JLabel lblPasso;
	private JLabel lblPasso_1;
	private JLabel lblPasso_2;

	public PrincipioFundamentalContagemInterface() {
		setBackground(Color.WHITE);
		setAlignmentY(Component.TOP_ALIGNMENT);
		setAlignmentX(Component.LEFT_ALIGNMENT);
		setLayout(null);
		setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));

		tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		tabbedPane.setBounds(12, 12, 956, 550);
		add(tabbedPane);

		JPanel panel_1 = new BackgroundPanel();
		panel_1.setBorder(new EtchedBorder(EtchedBorder.RAISED, null, null));
		panel_1.setLayout(null);
		panel_1.setBackground(new Color(255, 255, 255, 240));
		panel_1.setBounds(74, 12, 839, 105);
		tabbedPane.add(panel_1, "Enunciado");

		JPanel panel_1_1 = new JPanel();
		panel_1_1.setBounds(34, 26, 841, 472);
		panel_1_1.setBackground(new Color(255, 255, 255, 240));
		panel_1_1.setLayout(null);

		JTextPane txtpnEnunciado = new JTextPane();
		txtpnEnunciado.setFont(new Font("Dialog", Font.PLAIN, 14));
		txtpnEnunciado.setContentType("text/html");
		txtpnEnunciado.setOpaque(false);
		txtpnEnunciado.setBounds(32, 33, 771, 406);
		txtpnEnunciado.setEditable(false);
		txtpnEnunciado
				.setText("<body style='font: 13px dialog;'>" +
						"Clique na aba \"Exercício\" e calcule o arranjo, através do <b><u>princípio fundamental da contagem</u></b>, "
						+ "do exercício anterior :<br><br>"
						+ "<center><b>4(quatro) bandas</b> distintas concorrendo a <b>2(duas) vagas</b>,<br> "
						+ "com disitinção entre a primeira e segunda colocação.</center><br><br><hr><br>" +
						"<font size=4>O princípio fundamental da contagem"
						+ " nos diz que sempre devemos multiplicar os números de opções<br>"
						+ " entre as escolhas que podemos fazer. <br><br>"
						+ "Por exemplo, para montar um computador, temos:" +
						"<ul><li> 3 tipos de monitores</li><li>4 tipos de teclados</li><li>2 tipos de impressora e 3 tipos de CPU</li></ul> <br>"
						+ "Para saber o numero de diferentes possibilidades de computadores"
						+ "que podem ser montados com essas peças,"
						+ "somente multiplicamos as opções: <br><br>"
						+ "3 x 4 x 2 x 3 = 72 possibilidades de configurações diferentes.</font></body>");
		panel_1_1.add(txtpnEnunciado);
		panel_1.add(panel_1_1);

		panel = new BackgroundPanel();
		panel.setBounds(0, 0, 284, 420);
		panel.setBorder(new EtchedBorder(EtchedBorder.RAISED, null, null));
		panel.setLayout(null);
		tabbedPane.add(panel, "Exercício");

		panel_dica = new JPanel();
		panel_dica.setBounds(591, 25, 319, 466);
		panel.add(panel_dica);
		panel_dica.setBorder(new EtchedBorder(EtchedBorder.RAISED, null, null));
		panel_dica.setBackground(new Color(255, 255, 255, 240));
		panel_dica.setLayout(null);

		lblAjuda = new JLabel("");
		lblAjuda.setBounds(228, 12, 56, 50);
		panel_dica.add(lblAjuda);
		lblAjuda.setIcon(new ImageIcon(
				PrincipioFundamentalContagemInterface.class
						.getResource("/images/dica.png")));

		btnDica = new JButton("Mais Dicas!");
		btnDica.setBounds(12, 12, 114, 25);
		panel_dica.add(btnDica);

		txtpnDicas = new JTextPane();
		txtpnDicas.setBorder(new BevelBorder(BevelBorder.LOWERED, null,
				null, null, null));
		txtpnDicas.setContentType("text/html");
		txtpnDicas.setFont(new Font("Dialog", Font.PLAIN, 14));
		txtpnDicas.setEditable(false);
		txtpnDicas.setText("Caso precise de ajuda, clique em \"Mais Dicas!\".");
		txtpnDicas.setBounds(22, 75, 271, 356);
		panel_dica.add(txtpnDicas);

		panel_2 = new JPanel();
		panel_2.setBounds(31, 25, 536, 466);
		panel.add(panel_2);
		panel_2.setBorder(new EtchedBorder(EtchedBorder.RAISED, null, null));
		panel_2.setLayout(null);
		panel_2.setBackground(new Color(255, 255, 255, 240));

		JLabel lblNX = new JLabel("N  x  (    N    -   1    )   ");
		lblNX.setFont(new Font("Dialog", Font.PLAIN, 25));
		lblNX.setBounds(83, 178, 322, 31);
		panel_2.add(lblNX);

		textField = new JFormattedTextField(NumberFormat.getNumberInstance());
		textField.setFont(new Font("Dialog", Font.PLAIN, 25));
		textField.setBounds(52, 232, 63, 48);
		textField.setColumns(10);
		panel_2.add(textField);

		textField_1 = new JFormattedTextField(NumberFormat.getNumberInstance());
		textField_1.setFont(new Font("Dialog", Font.PLAIN, 25));
		textField_1.setBounds(168, 232, 63, 48);
		textField_1.setColumns(10);
		panel_2.add(textField_1);

		JLabel lblX_2 = new JLabel("=           x  (          -   1    )");
		lblX_2.setFont(new Font("Dialog", Font.PLAIN, 25));
		lblX_2.setBounds(12, 238, 323, 37);
		panel_2.add(lblX_2);

		JLabel lblNewLabel = new JLabel("");
		lblNewLabel.setLocation(135, 0);
		lblNewLabel.setSize(96, 179);
		lblNewLabel.setIcon(new ImageIcon(
				PrincipioFundamentalContagemInterface.class
						.getResource("/images/podiomenor.png")));
		panel_2.add(lblNewLabel);

		btnValida1 = new JButton("Validar");
		btnValida1.setOpaque(false);
		btnValida1.setFont(new Font("Dialog", Font.BOLD, 14));
		btnValida1.setActionCommand("Validar");
		btnValida1.setLocation(353, 241);
		btnValida1.setSize(96, 37);
		panel_2.add(btnValida1);

		panel_3 = new JPanel();
		panel_3.setOpaque(false);
		panel_3.setBorder(null);
		panel_3.setVisible(false);
		panel_3.setBounds(12, 292, 337, 75);
		panel_2.add(panel_3);
		panel_3.setLayout(null);

		textField_4 = new JFormattedTextField(NumberFormat.getNumberInstance());
		textField_4.setBounds(143, 19, 63, 48);
		panel_3.add(textField_4);
		textField_4.setFont(new Font("Dialog", Font.PLAIN, 25));
		textField_4.setColumns(10);

		textField_3 = new JFormattedTextField(NumberFormat.getNumberInstance());
		textField_3.setBounds(38, 19, 63, 48);
		panel_3.add(textField_3);
		textField_3.setFont(new Font("Dialog", Font.PLAIN, 25));
		textField_3.setColumns(10);

		lblX = new JLabel("=           x ");
		lblX.setFont(new Font("Dialog", Font.PLAIN, 25));
		lblX.setBounds(-2, 25, 140, 37);
		panel_3.add(lblX);
		
		lblPasso_2 = new JLabel("Passo 2");
		lblPasso_2.setBounds(0, 0, 70, 15);
		panel_3.add(lblPasso_2);

		panel_4 = new JPanel();
		panel_4.setOpaque(false);
		panel_4.setVisible(false);
		panel_4.setBounds(12, 379, 337, 75);
		panel_2.add(panel_4);
		panel_4.setLayout(null);

		textField_2 = new JFormattedTextField(NumberFormat.getNumberInstance());
		textField_2.setBounds(37, 22, 63, 48);
		panel_4.add(textField_2);
		textField_2.setFont(new Font("Dialog", Font.PLAIN, 25));
		textField_2.setColumns(10);

		label = new JLabel("=");
		label.setBounds(-3, 28, 28, 37);
		panel_4.add(label);
		label.setFont(new Font("Dialog", Font.PLAIN, 25));
		
		lblPasso_1 = new JLabel("Passo 3");
		lblPasso_1.setBounds(0, 1, 70, 15);
		panel_4.add(lblPasso_1);
		
		lblPasso = new JLabel("Passo 1");
		lblPasso.setBounds(12, 214, 70, 15);
		panel_2.add(lblPasso);

		controle();
		balloonTip1();
		balloonTip2();
		balloonTip1_1();
		balloonTip2_1();
		balloonTip3();
	}

	public void balloonTip1() {
		BalloonTipStyle roundedLook = new RoundedBalloonStyle(10, 10,
				Color.WHITE, Color.LIGHT_GRAY);

		balloonTip1 = new BalloonTip(
				textField,
				new JLabel(
						"<html>Quantas bandas <br/> estão concorrendo um lugar no pódio?</html>"),
				roundedLook, Orientation.RIGHT_BELOW, AttachLocation.ALIGNED,
				30, 20, false);

		balloonTip1.setOpaque(false);
		balloonTip1.setVisible(false);

	}

	public void balloonTip1_1() {
		BalloonTipStyle roundedLook = new RoundedBalloonStyle(10, 10,
				Color.WHITE, Color.LIGHT_GRAY);

		balloonTip1_1 = new BalloonTip(
				textField_3,
				new JLabel(
						"<html>Quantas bandas <br/>podem ganhar o primeiro lugar?</html>"),
				roundedLook, Orientation.RIGHT_BELOW, AttachLocation.ALIGNED,
				30, 20, false);

		balloonTip1_1.setOpaque(false);
		balloonTip1_1.setVisible(false);
	}

	public void balloonTip2() {
		BalloonTipStyle roundedLook2 = new RoundedBalloonStyle(10, 10,
				Color.WHITE, Color.LIGHT_GRAY);

		balloonTip2 = new BalloonTip(
				textField_1,
				new JLabel(
						"<html>Quantas bandas <br/> estão concorrendo um lugar no pódio?</html>"),
				roundedLook2, Orientation.LEFT_BELOW, AttachLocation.ALIGNED,
				20, 20, false);

		balloonTip2.setOpaque(false);
		balloonTip2.setVisible(false);
	}

	public void balloonTip2_1() {
		BalloonTipStyle roundedLook2 = new RoundedBalloonStyle(10, 10,
				Color.WHITE, Color.LIGHT_GRAY);

		balloonTip2_1 = new BalloonTip(
				textField_4,
				new JLabel(
						"<html>Após a escolha do primeiro lugar, <br/>quantas bandas sobraram <br/>para concorrer o segundo lugar?</html>"),
				roundedLook2, Orientation.LEFT_BELOW, AttachLocation.ALIGNED,
				20, 20, false);

		balloonTip2_1.setOpaque(false);
		balloonTip2_1.setVisible(false);
	}

	public void balloonTip3() {
		BalloonTipStyle roundedLook3 = new RoundedBalloonStyle(10, 10,
				Color.WHITE, Color.LIGHT_GRAY);

		balloonTip3 = new BalloonTip(textField_2, new JLabel(
				"Insira a resposta aqui."), roundedLook3,
				Orientation.LEFT_BELOW, AttachLocation.ALIGNED, 20, 20, false);

		balloonTip3.setOpaque(false);
		balloonTip3.setVisible(false);
	}

	public void balloonTipAction(BalloonTip b, boolean a) {
		b.setVisible(a);
		repaint();
	}

	public void controle() {
		btnDica.addMouseListener(new MouseAdapter() {

			public void mouseClicked(MouseEvent arg0) {
				dicas();
			}

		});


		textField.addFocusListener(new FocusListener() {
			public void focusGained(FocusEvent e) {
				balloonTipAction(balloonTip1, true);
			}

			public void focusLost(FocusEvent e) {
				balloonTipAction(balloonTip1, false);
			}

		});

		textField_3.addFocusListener(new FocusListener() {
			public void focusGained(FocusEvent e) {
				balloonTipAction(balloonTip1_1, true);
			}

			public void focusLost(FocusEvent e) {
				balloonTipAction(balloonTip1_1, false);
			}
		});

		textField_1.addFocusListener(new FocusListener() {
			public void focusGained(FocusEvent e) {
				balloonTipAction(balloonTip2, true);
			}

			public void focusLost(FocusEvent e) {
				balloonTipAction(balloonTip2, false);
			}

		});

		textField_4.addFocusListener(new FocusListener() {
			public void focusGained(FocusEvent e) {
				balloonTipAction(balloonTip2_1, true);
			}

			public void focusLost(FocusEvent e) {
				balloonTipAction(balloonTip2_1, false);
			}

		});

		textField_2.addFocusListener(new FocusListener() {
			public void focusGained(FocusEvent e) {
				balloonTipAction(balloonTip3, true);
			}

			public void focusLost(FocusEvent e) {
				balloonTipAction(balloonTip3, false);
			}

		});

		btnValida1.addMouseListener(new MouseAdapter() {

			public void mouseClicked(MouseEvent arg0) {
				valida();
			}
		});
	}
	
	public void dicas() {
		PrincipioFundamentalContagemDicas mult = new PrincipioFundamentalContagemDicas();

		mult.setErros(erros);
		mult.setDica(txtpnDicas);
		mult.setOpcao(textField, 0);
		mult.setOpcao(textField_1, 1);
		mult.setOpcao(textField_3, 2);
		mult.setOpcao(textField_4, 3);

		mult.setResposta(textField_2);

		mult.setPanel(panel_2, 0);
		mult.setPanel(panel_3, 1);
		mult.setPanel(panel_4, 2);

		regrasDicas.tell(mult);
		regrasDicas.run();
		regrasDicas.retract(mult);
		repaint();

	}
	public void valida() {
		PrincipioFundamentalContagem mult = new PrincipioFundamentalContagem();

		mult.setOpcao(textField, 0);
		mult.setOpcao(textField_1, 1);
		mult.setOpcao(textField_3, 2);
		mult.setOpcao(textField_4, 3);

		mult.setResposta(textField_2);
		mult.setErros(erros);
		mult.setBtnValida(btnValida1);
		mult.setPanel(panel_2, 0);
		mult.setPanel(panel_3, 1);
		mult.setPanel(panel_4, 2);

		regras.tell(mult);
		regras.run();
		regras.retract(mult);

		repaint();
	}
	
	public int getValueResposta() {
		return Integer.parseInt(textField_2.getText());
	}
	
	public JPanel getPanelResposta() {
		return panel_4;
	}
	
}
